<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
$ui = new ui();
$id = intval( $_GET['id'] );
if ( $id == 0 )
{
    exit( "You not input id!" );
}
$acl = 2;
$funlist = "<a href=\"?op=genagent&fp=agent&cmd=list&enabled=1\">启用列表</a>".( "<a href=\"?op=genagent&fp=agent&cmd=list&enabled=0\">停用列表</a>" )."<a href=\"?op=genagent&fp=agent&cmd=add\">新增</a>";
echo "<table bgcolor=#B3B3B3 border=0 cellpadding=0 cellspacing=1 width=750><tr><td>".( "<table class=table0 width='100%' border=0 cellpadding=0 cellspacing=0><tr><td class=title0>" )."<table width=100% border=0 cellpadding=1 cellspacing=0><tr><td>代理管理 ".( 0 < $id ? L_EDIT : L_ADD )."<td align=right>".$funlist."</table></table></table><br>";
if ( 0 < $id )
{
    if ( $curruser['acl'] < 3 )
    {
        diemsg( "你没有权限存取！" );
    }
    if ( $curruser['acl'] < 255 )
    {
        $users = getuserlist( $curruser['id'], $acl );
        if ( $users == "" )
        {
            diemsg( "不是你的代理商！" );
        }
        if ( strpos( ",".$users.",", ",".$id."," ) === false )
        {
            diemsg( "不是你的代理商！" );
        }
    }
}
$rs = getdb( "select * from k_user where acl=".$acl." and id=".$id );
if ( $rs->eof )
{
    diemsg( L_ERRMEMID );
}
if ( ispost( ) )
{
    foreach ( $g_betname as $key => $value )
    {
        $rs1 = getdb( "select * from amounts where user_id=".$rs->f['prev_id']." and bettype=".$key );
        $amount1 = intval( $_POST["amount1".$key] );
        $amount2 = intval( $_POST["amount2".$key] );
        $back = 100 - floatval( $_POST["back".$key] );
        if ( $rs1->f['amount1'] < $amount1 )
        {
            echo "<li>".$value." 额度不能大於上层！";
        }
        else if ( $rs1->f['amount2'] < $amount2 )
        {
            echo "<li>".$value." 额度不能大於上层！";
        }
        else if ( $back < $rs1->f['back'] )
        {
            echo "<li>".$value." 退水大能小於上层！";
        }
        else if ( $back < 0 )
        {
            echo "<li>".$value." 退水不能小於 0！";
        }
        else if ( 100 < $back )
        {
            echo "<li>".$value." 退水不能大於 100！";
        }
        else
        {
            $rs1->close();
            $rs1 = getdb( "select * from amounts where user_id={$id} and bettype={$key}" );
            if ( $rs1->eof )
            {
                $sql = "insert into amounts (user_id,bettype,amount1,amount2,back) values ({$id}, {$key}, {$amount1}, {$amount2}, {$back})";
            }
            else
            {
                $sql = "update amounts set amount1={$amount1}, amount2={$amount2}, back={$back} where user_id={$id} and bettype={$key}";
            }
            $rs1->close();
            getdb( $sql );
        }
    }
}
$tpl = new Template2();
$tpl->load( "main", "amount.htm" );
$tpl->set( "action", "?".$_SERVER['QUERY_STRING'] );
$myamount = GetAmountSetting( $id, 0, 0 );
$tpl->set( "account", $rs->f['account'] );
$tpl->set( "name", $rs->f['name'] );
$tpl->set( "id", $id );
$tpl->set( "amount", $rs->f['amount'] );
$list = "";
foreach ( $g_betname as $key => $value )
{
    $list .= "<tr class=td2>";
    $list .= "<td class=td1>".$value;
    $list .= "<td><input size=5 onkeypress=\"return OnlyFloat()\" name=back".$key." value=".round( 100 - $myamount[$key][0], 1 )."></td>";
    $list .= "<td><input size=5 onkeypress=\"return OnlyInt()\" name=amount1".$key." value=".$myamount[$key][1]."></td>";
    $list .= "<td><input size=5 onkeypress=\"return OnlyInt()\" name=amount2".$key." value=".$myamount[$key][2]."></td>";
    $list .= "</tr>\n";
}
$tpl->set( "BETAMOUNTS", $list );
$tpl->set( "submit", 0 < $id ? L_OK : L_ADD );
$tpl->show( "main" );
?>